import jsCookie from 'js-cookie'

const utils = {
  hostname(prefix = '') {
    const hn = location.hostname
    const pathName = location.pathname.match(/(.*?)\/m\//i)[1] || ''
    if (hn === 'minih5.retailo2o.com' || hn === 'minih5.linkkids.cn') {
      return `//${window.location.hostname}${pathName}`
    } else {
      return `//${window.location.hostname}`
    }
  },
  login() {
    let url = process.env.NODE_ENV === 'production' ? '//rmc.linkkids.cn' : '//test.mmc.linkkids.cn'
    location.href = url
  },
  title(titlename) {
    try {
      document.title = titlename
      window.kwapp && kwapp.setTitle(titlename) //eslint-disable-line
    } catch (e) {
      window.kwapp && kwapp.onReady(() => { //eslint-disable-line
        kwapp.setTitle(titlename) //eslint-disable-line
      })
    }
  },
  dateFormat(t, formater) {
    if (!t) {
      return ''
    }
    if (typeof t === 'string') {
      t = t.replace(/-/g, '/')
    }
    const time = new Date(t)
    const year = time.getFullYear()
    const month = `0${time.getMonth() + 1}`.slice(-2)
    const date = `0${time.getDate()}`.slice(-2)
    const hours = `0${time.getHours()}`.slice(-2)
    const minutes = `0${time.getMinutes()}`.slice(-2)
    const seconds = `0${time.getSeconds()}`.slice(-2)
    return formater.replace('YY', year)
      .replace('MM', month)
      .replace('DD', date)
      .replace('hh', hours)
      .replace('mm', minutes)
      .replace('ss', seconds)
  },
  getImgSize(file) {
    if (!file) return
    return new Promise((resolve, reject) => {
      const reader = new FileReader()
      reader.onload = async(e) => {
        const data = e.target.result
        const image = new Image()
        image.onload = () => {
          resolve(image)
        }
        image.src = data
      }
      reader.readAsDataURL(file)
    })
  }

}
export default utils
